package org.example.cake.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.example.cake.common.Rep;
import org.example.cake.entity.Cake;
import org.example.cake.entity.Ordercake;
import org.example.cake.service.CakeService;
import org.example.cake.service.OrdercakeService;
import org.example.cake.service.PaycakeidSercice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/ordercake")
public class OrdercakeController {
    @Autowired
    private OrdercakeService ordercakeService;
    @Autowired
    private PaycakeidSercice paycakeidSercice;
    @Autowired
    private CakeService cakeService;
    /**
     * 根据用户id查询不同订单状态的订单信息
     *
     * @param userid 用户id
     * @return 集合
     */
    @GetMapping("/selectOrdercake")
    public Rep selectOrdercake(@RequestParam("userid") Integer userid) {
        List<Ordercake> ordercakeList = this.ordercakeService.selectOrdercake(userid);
        //根据支付信息id查询蛋
        for (Ordercake ordercake : ordercakeList) {
            List<Cake> cakeList=new ArrayList<>();
            //根据支付信息获取cakeid
            List<Integer> cakeid=paycakeidSercice.selectByckaeid(ordercake.getPayid());
            //根据payid查询蛋糕的总数
//            Integer count=paycakeidSercice.getcakecount(ordercake.getPayid());
//            ordercake.setOrderecount(count);
            //根据蛋糕id查询蛋糕信息
            for (Integer id : cakeid) {
                Cake cake=cakeService.getCake(id);
                ordercake.setCakeid(cake.getCakeid());
                ordercake.setCakename(cake.getCakename());
                ordercake.setCakedesc(cake.getCakedesc());
                ordercake.setCakeprice(cake.getCakeprice());
                ordercake.setCakeurl(cake.getCakeurl());
                cakeList.add(cake);
            }
            if(cakeList.size()>0){
                cakeList.remove(cakeList.size()-1);
                ordercake.setCakes(cakeList);
            }

        }

        return new Rep().addData("ordercakeList", ordercakeList);
    }

    /**
     * 点击申请退款就修改退款的类型从0[未退款]改为1[退款中]
     *
     * @param orderid 订单id
     * @return 收影响的行数
     */
    @PostMapping("/applyrefund")
    public int applyrefund(@RequestParam("orderid") Integer orderid,@RequestParam("userreason") String userreason) {
        int applyrefund = this.ordercakeService.applyrefund(orderid,userreason);
        if (applyrefund > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 根据用户id查询他申请提交的订单
     *
     * @param userid 用户id
     * @return 订单集合
     */
    @GetMapping("/selectapplyfund")
    public Rep selectapplyfund(@Param("userid") Integer userid,Integer page, Integer limit
        ,@RequestParam(required = false) String cakeName
        ,@RequestParam(required = false) String orderstatus,
         @RequestParam(required = false) String ordernum) {
        page = page == null ? 1 : page;
        limit = limit == null ? 10 : limit;
        PageHelper.startPage(page, limit);
        List<Ordercake> selectapplyfund = this.ordercakeService.selectapplyfund(userid,cakeName,orderstatus,ordernum);
        PageInfo<Ordercake> pageInfo = new PageInfo<>(selectapplyfund);
        return new Rep().addData("selectapplyfund", pageInfo);
    }
    @PostMapping("/updateRefundsStatus")
    public Rep updateRefundsStatus(@RequestParam("orderid") Integer orderid,@RequestParam("refund")Integer refund) {
        System.out.println("orderid = " + orderid);
        System.out.println("refund = " + refund);
        return new  Rep().addData("updateRefundsStatus", this.ordercakeService.updateRefundsStatus(orderid, refund));
    }

    /**
     * 获取订单总数
     * @return
     */
    @GetMapping("getcountdayandmonth")
    public  Rep getcountdayandmonth(){
        //获取当前总数
        Integer todaycount= ordercakeService.getTodayOrderCount();
        //获取当月总数
        Integer monthCount=ordercakeService.getMonthOrderCount();
        //获取当日金额
        String todaymoney=ordercakeService.gettodayMoney();
        //获取当月金额
        String monthmoney=ordercakeService.getmonthMoney();
        return new Rep().addData("todaycount", todaycount).addData("monthCount", monthCount).addData("monthmoney", monthmoney).addData("todaymoney", todaymoney);
    }

    /**
     * 获取7日数据
     * @return
     */
    @GetMapping("getdata")
    public Rep getdata(){
        //获取前七天的日子
        // 获取当前日期
        LocalDate today = LocalDate.now();
        //存日期
        List<String> lastSevenDays = new ArrayList<>();
        //存总数
        List<Integer> datacount=new ArrayList<>();
        // 生成最近7天的日期
        for (int i = 7; i > 0; i--) {
            LocalDate date = today.minusDays(i); // 从今天往前推i天
            lastSevenDays.add(date.format(DateTimeFormatter.ISO_LOCAL_DATE));
        }
        //根据日期获取总数
        for (int i = 0; i < lastSevenDays.size(); i++) {
            Integer todaycount= ordercakeService.getTodayOrderCount(lastSevenDays.get(i));
            datacount.add(todaycount);
            lastSevenDays.set(i,lastSevenDays.get(i).substring(5,lastSevenDays.get(i).length()));
        }
        return new Rep().addData("datacount", datacount).addData("lastSevenDays", lastSevenDays);
    }
    /**
     * 根据用户id查询他申请提交的订单小程序端
     * @param userid 用户id
     * @return 订单集合
     */
    @GetMapping("/selectapplyfundFront")
    public Rep selectapplyfundFront(@Param("userid") Integer userid){
        List<Ordercake> ordercakeList = ordercakeService.selectapplyfundFront(userid);
        return new Rep().addData("ordercakeList", ordercakeList);
    }
    /**
     * 商家不同意退款方法
     * @param shopreply 商家回复
     * @param orderid 订单id
     * @return 受影响的行数
     */
    @PostMapping("/refundReply")
    public Integer refundReply(@RequestParam("shopreply") String shopreply, @RequestParam("orderid") Integer orderid) {
        Integer refundReply = ordercakeService.refundReply(shopreply, orderid);
        System.out.println("商家不同意退款方法refundReply = " + refundReply);
        if (refundReply != null) {
            return refundReply.intValue() > 0 ? 1 : 0;
        } else {
            return 0; // 或根据业务返回默认值
        }
    }
    /**
     * 查询今日的订单
     * @return 订单集合
     */
    @GetMapping("/selectTodayOrder")
    public Rep selectTodayOrder(){
        List<Ordercake> ordercakeList = ordercakeService.selectTodayOrder();
        return new Rep().addData("ordercakeList", ordercakeList);
    }
    /**
     * 用户确认收货
     * @param orderid  订单id
     * @return 受影响的行数
     */
    @PostMapping("/confirmGoods")
    public int confirmGoods(@RequestParam("orderid") Integer orderid){
        int confirmGoods = ordercakeService.confirmGoods(orderid);
        if (confirmGoods > 0) {
            return confirmGoods;
        }else {
            return 0;
        }
    }
    /**
     * 用户删除订单
     * @param orderid 订单id
     * @return 受影响的行数
     */
    @PostMapping("/deleteGoods")
    public int deleteGoods(@RequestParam("orderid") Integer orderid){
        int deleteGoods = ordercakeService.deleteGoods(orderid);
        if (deleteGoods > 0) {
            return deleteGoods;
        }else {
            return 0;
        }
    }
    /**
     * 接单
     * @param orderid 订单id
     * @return 受影响的行数
     */
    @PostMapping("/updateGoods")
        public int updateGoods(@RequestParam("orderid") Integer orderid){
        int updateGoods = ordercakeService.updateGoods(orderid);
        if (updateGoods > 0) {
            return updateGoods;
        }else {
            return 0;
        }
    }


}
